import request from './interceptors';
import Qs from 'qs';
interface fetchType {
  method: string, url: string, params: any
}
const Methods = (method: string, url: string, params: any) => {
  return new Promise((resolve, reject) => {
    request[method](url, {
      ...params,
    })
      .then((response: any) => {
        resolve(response);
      })
      .catch((error: any) => {
        console.log(error);

        reject(error);
      });
  });
};
const fetchApi = (requestParams: fetchType) => {
  const { method, url, params } = requestParams;
  if (method === 'get') {
    return Methods('get', url, {
      params: params
    });
  } else {

    return Methods('post', url, {
      data: Qs.stringify(params),
      headers: {
        "Authorization": localStorage.getItem('set_token'),

        "Content-type": "application/x-www-form-urlencoded;charset=utf-8"
      },
    });
  }



}
const Get = (url: string, params?: any) => {
  return Methods('get', url, {
    params: params
  });
};

const Post = (url: string, params: any) => {
  return Methods('post', url, {
    data: params
  });
};

export { Get, Post, fetchApi };
